<?php

namespace App\Http\Service\Customers;

use App\Facade\UserFuncFacade;
use App\Func\Company\CompanyFunc;
use App\Func\Customer\CustomerFunc;
use App\Func\File\FileFunc;
use App\Func\System\OperateLogFunc;
use App\Func\System\StrFunc;
use App\Func\User\UserFunc;
use App\Http\Service\BasicInterfaceService;
use App\Models\Customers\CustomersModel;
use App\Models\Customers\SignDataModel;
use Illuminate\Support\Facades\DB;

class CustomersService implements BasicInterfaceService
{

    public function getList(array $prams)
    {

    }

    public function dispose($data): mixed
    {

    }

    public function listSelectFilters($query, $filters)
    {

    }

    public function getSelect()
    {

    }

    public function getSelectHasPage($prams)
    {

    }

    public function getInfo($id)
    {

    }

    public function create($data)
    {

    }

    public function fakeDelete($id)
    {

    }

    public function fakeDeleteByIds($ids)
    {

    }

    public function delete($id)
    {

    }

    public function update($data)
    {

    }

    public function account_register($data)
    {
        // 校验验证码和key
        if (!captcha_api_check($data['captcha'] , $data['key'])){
            s_fail_return(null,trans('message.error',[
                'name' => trans('key.captcha')
            ]));
        }
        try {
            DB::beginTransaction();
            // 加密密码
            $puPvKey = rsa_pu_pv_key();
            $hashPassword = UserFuncFacade::getHashPassByUser($data['password']);
            $insertData = [
                'name' => (new StrFunc())->getRandomZhName(),
                'com_id' => (new CompanyFunc())->getCustomerComId(),
                'avatar_id' => -1,
                'account' => $data['account'],
                'password' => $hashPassword,
                'union_id' => (\Symfony\Component\Uid\UuidV4::v6()->toRfc4122()),
                'enable_status' => 1,
                'status' => 1,
                'pu_key' => $puPvKey['pu_key'],
                'pv_key' => $puPvKey['pv_key'],
                'created_at' => date('Y-m-d h:i:s',time()),
                'updated_at' => date('Y-m-d h:i:s',time()),
            ];
            $customersModel = new CustomersModel();
            $customerId = $customersModel->insertGetId($insertData);
            // 添加操作记录
            OperateLogFunc::addCustomersOperate(
                'CustomersService',
                $customerId,
                'create',
                uid:$customerId);
            DB::commit();
            s_c_return(['user_id'=>$customerId],trans('message.register_ok'));
        }catch (\Exception $exception){
            DB::rollBack();
            s_fail_return(null,trans('message.register_err'));
        }
    }

    public function getCurrentUserInfo()
    {
        $res = (new CustomerFunc())->getLoginInfo();
        $res['avatar_files'] = (new FileFunc())->getFileInfoByFileIdToWeb($res['avatar_id']);
        s_c_return($res,trans('message.get_detail_ok',[
            'name' => trans('key.customer')
        ]));
    }

    // 签到
    public function signDate()
    {
//        $info = (new CustomerFunc());
        $info = (new UserFunc())->getLoginInfo();
        $signDate = new SignDataModel();
        // 校验当前用户是否签过到
        $sign = $signDate->where('user_id',$info['user_id'])->where('created_at',">=",date("Y-m-d"))->first();
        if($sign){
            s_fail_return(null,trans('message.signed_in_today'));
        }
        $insertData = [
            'user_id' => $info['user_id'],
            'status' => 1,
            'created_at' => date("Y-m-d h:i:s")
        ];

        $signDate->insertGetId($insertData);
        s_c_return(null,trans('message.signed_ok'));
    }

}
